package com.sunday.android.fangdaifu.bukong.activity.index;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.adapter.BuilderDetailImageAdapter;
import com.sunday.android.fangdaifu.bukong.entity.IndexHouseDetail;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
*建筑详情
* */
public class BuilderDetailActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tvName)
    TextView tvName;
    @Bind(R.id.tvTime)
    TextView tvTime;
    @Bind(R.id.tvAddress)
    TextView tvAddress;
    @Bind(R.id.tvManage)
    TextView tvManage;
    @Bind(R.id.tvCount)
    TextView tvCount;
    @Bind(R.id.tvEvr)
    TextView tvEvr;
    @Bind(R.id.tvDown)
    TextView tvDown;
    @Bind(R.id.tvBreak)
    TextView tvBreak;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.tvInfo)
    TextView tvInfo;
    @Bind(R.id.tvBtn)
    TextView tvBtn;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private long id;
    private IndexHouseDetail detail;
    private BuilderDetailImageAdapter adapter;
    private ArrayList<String> imgList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_builder_detail);
        ButterKnife.bind(this);
        id = getIntent().getLongExtra("id",0);
        initView();
        getData();
    }

    private void initView(){
        titleView.setText("项目建筑详情");
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new BuilderDetailImageAdapter(mContext,imgList);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    private void showData(){
        String str1 = String.format("倾斜 %d",detail.getHorizontal());
        String str2 = String.format("沉降 %d",detail.getSettlement());
        String str3 = String.format("裂缝(位移) %d",detail.getCrack());
        String str4 = String.format("负责人 %s",detail.getNickName());
        tvName.setText(detail.getHouseName());
        tvEvr.setText(SpannalbeStringUtils.setTextColor(str1, Color.parseColor("#333333"),3,str1.length()));
        tvDown.setText(SpannalbeStringUtils.setTextColor(str2, Color.parseColor("#333333"),3,str2.length()));
        tvBreak.setText(SpannalbeStringUtils.setTextColor(str3, Color.parseColor("#333333"),7,str3.length()));
        tvTime.setText(String.format("项目周期  %s 至 %s",detail.getStartTime(),detail.getEndTime()));
        tvManage.setText(SpannalbeStringUtils.setTextColor(str4, Color.parseColor("#333333"),4,str4.length()));
        tvAddress.setText(detail.getAddress());
        tvInfo.setText(detail.getInfo());
        imgList.clear();
        imgList.addAll(detail.getImageList());
        adapter.notifyDataSetChanged();
    }

    private void getData() {
        loading();
        Call<ResultDO<IndexHouseDetail>> call = ApiClient.getApiAdapter().AdItemDetail(id);
        call.enqueue(new Callback<ResultDO<IndexHouseDetail>>() {
            @Override
            public void onResponse(Call<ResultDO<IndexHouseDetail>> call, Response<ResultDO<IndexHouseDetail>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    detail = response.body().getResult();
                    showData();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<IndexHouseDetail>> call, Throwable t) {
                if (isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
            }
        });
    }

    private void loading(){
        if (!refreshLayout.isRefreshing()){
            refreshLayout.setRefreshing(true);
        }
    }

    @OnClick({R.id.tvAddress, R.id.tvBtn, R.id.tvInfoMore})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvAddress:
                intent = new Intent(mContext,BuilderMapActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("detail",detail);
                intent.putExtra("detail",bundle);
                startActivity(intent);
                break;
            case R.id.tvBtn:
                if (detail==null){
                    return;
                }
                intent = new Intent(mContext,InstallEquipmentActivity.class);
                intent.putExtra("id",detail.getItemId());
                startActivity(intent);
                break;
            case R.id.tvInfoMore:
                break;
        }
    }
}
